<?php

namespace app\admin\controller\dingdan;

use app\common\controller\Backend;


/**
 * 套餐项目
 *
 * @icon  fa fa-tv
 */
class Item extends Backend
{
    /**
     * View
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,null);
            $list = db('package_item')
                ->where($where)
                ->order($sort, 'desc')
                ->paginate($limit);

            $data = $list->items();
            foreach ($data as &$item){
                $item['package_name'] = db('package')->where('id',$item['package_id'])->value('name');
            }
            $result = array("total" => $list->total(), "rows" => $data,"extend"=>[]);

            return json($result);
        }

        return $this->view->fetch();

    }

    /**
     * Add
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if(empty($params['name'])){
                $this->error("名字不为空！");
            }

            if(empty($params['package_id'])){
                $this->error("请选择套餐！");
            }
            $params['create_time'] = date("Y-m-d H:i:s");
            unset($params['id']);
            db('package_item')->insertGetId($params);
            $this->success("添加成功");
        }

        $packageData = db('package')->where('id','>',0)->select();
        $this->view->assign('id',0);
        $this->view->assign('data',[]);
        $this->view->assign('packageData',$packageData);
        $this->view->assign('type',1);
        return $this->view->fetch('add');

    }

    /**
     * Edit
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if(empty($params['name'])){
                $this->error("名字不为空！");
            }
            if(empty($params['package_id'])){
                $this->error("请选择套餐！");
            }
            unset($params['id']);
            db('package_item')->where('id',$ids)->update($params);
            $this->success("修改成功");
        }


        $data = db('package_item')->where('id',$ids)->find();
        $packageData = db('package')->where('id','>',0)->select();
        $this->view->assign('id',$ids);
        $this->view->assign('data',$data);
        $this->view->assign('packageData',$packageData);
        $this->view->assign('type',$data['type']);
        return $this->view->fetch('add');
    }

    /**
     * Del
     */
    public function del($ids = null)
    {
        if(empty($ids))
            $this->error(__('Error Parame'));

        $ids = explode(',',$ids);
        $error = [];
        foreach ($ids as $id){
            db('package_item')->where('id',$id)->delete();
        }
        $this->success("删除成功");
    }
    /**
     * 选择套餐
     */
    public function ajaxPackage(){
        $name = input('name','');
        $filed = "id,name";

        //        $word = $this->request->param()['q_word'][0];
        $page = $this->request->param('pageNumber');
        $limit = $this->request->param('pageSize');
        $page = $page ? $page : 1;
        $limit = $limit ? $limit : 20;

        if(empty($name)){
            $total = db('package')->where('id','>',0)->count();
            $data = db('package')
                ->where('id','>',0)
                ->field($filed)
                ->order('id desc')
                ->limit($limit)
                ->page($page)
                ->select();
        }else{

            $total = db('package')->where("name like '%{$name}%'")->count();
            $data = db('package')
                ->field($filed)
                ->where("name like '%{$name}%'")
                ->order('id desc')
                ->limit($limit)
                ->page($page)
                ->select();
        }

        $reData = ['list'=>[],'total'=>0];
        $list = [];
        foreach ($data as $item){
            $list[] = $item;
        }
        $reData['list'] = $list;
        $reData['total'] = $total;
        echo json_encode($reData);die();
    }
}
